<?php
/**
 * Created by PhpStorm.
 * User: freethy
 * Date: 2018-05-11
 * Time: 11:02
 */

function formatDate($s, $format = "Y-m-d H:i:s")
{
    if ($s) {
        $s = date($format, $s);
        return $s;
    }
}

function getClientIP()
{
    if (getenv("HTTP_CLIENT_IP")) {
        $ip = getenv("HTTP_CLIENT_IP");
    } elseif (getenv("HTTP_X_FORWARDED_FOR")) {
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    } elseif (getenv("REMOTE_ADDR")) {
        $ip = getenv("REMOTE_ADDR");
    } else {
        $ip = "Unknow";
    }
    return $ip;
}

function startWith($str, $needle)
{
    if ($str) {
        return strpos($str, $needle) === 0;
    } else {
        return false;
    }
}

function endWith($haystack, $needle)
{
    $length = strlen($needle);
    if ($length == 0) {
        return true;
    }
    return (substr($haystack, -$length) === $needle);
}

function startWiths($str, $arr)
{
    for ($i = 0; $i < count($arr); $i++) {
        $b = startWith($str, $arr[$i]);
        if ($b) {
            return true;
        }
    }
    return false;
}

/**
 * 发送HTTP请求方法
 * @param string $url 请求URL
 * @param array $params 请求参数
 * @param string $method 请求方法GET/POST
 * @return array  $data   响应数据
 *
 * //定义一个要发送的目标URL；
 * $url = "https://www.xxx.com";
 * //定义传递的参数数组；
 * $data['aaa']='aaaaa';
 * $data['bbb']='bbbb';
 * //定义返回值接收变量；
 * $httpstr = http($url, $data, 'POST', array("Content-type: text/html; charset=utf-8"));
 */
function http($url, $params, $method = 'GET', $header = array(), $multi = false)
{
    $opts = array(
        CURLOPT_TIMEOUT => 300,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_HTTPHEADER => $header
    );
    /* 根据请求类型设置特定参数 */
    switch (strtoupper($method)) {
        case 'GET':
            $opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
            break;
        case 'POST':
            //判断是否传输文件
            $params = $multi ? $params : http_build_query($params);
            $opts[CURLOPT_URL] = $url;
            $opts[CURLOPT_POST] = 1;
            $opts[CURLOPT_POSTFIELDS] = $params;
            break;
        default:
            throw new Exception('不支持的请求方式！');
    }
    /* 初始化并执行curl请求 */
    $ch = curl_init();
    curl_setopt_array($ch, $opts);
    $data = curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch);
    if ($error) throw new Exception('请求发生错误：' . $error);
    return $data;
}


//by ponley
function uuid()
{
    return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
}





function get_date_time()
{
    return date('Y-m-d H:i:s');
}

function get_date()
{
    return date('Y-m-d');
}



//操作失败返回
function error_msg($msg,$is_fall=1)
{
    if($is_fall){
        $msg .= ',操作失败';
    }
    return json(['code' => -1, 'msg' => $msg]);
}

//操作成功返回
function success_msg()
{
    return json(['code' => 0, 'msg' => '操作成功']);
}

//操作详情返回
function info_msg($data)
{
    return json(['code' => 0, 'msg' => '获取成功', 'data' => $data]);
}

//操作详情返回
function table_msg($data, $count)
{
    return json(['code' => 0, 'msg' => '获取成功', 'total' => $count, 'rows' => $data]);
}




//判断是否是移动端，是返回true
function is_mobile()
{
    $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
    $is_pc = (strpos($agent, 'windows nt')) ? true : false;
    $is_mac = (strpos($agent, 'mac os')) ? true : false;
    $is_iphone = (strpos($agent, 'iphone')) ? true : false;
    $is_android = (strpos($agent, 'android')) ? true : false;
    $is_ipad = (strpos($agent, 'ipad')) ? true : false;


    if ($is_pc) {
        return false;
    }

    if ($is_mac) {
        return true;
    }

    if ($is_iphone) {
        return true;
    }

    if ($is_android) {
        return true;
    }

    if ($is_ipad) {
        return true;
    }
}

//获取当前日期
function get_current_date(){
    if(intval(date('H')) >= 8){
        $current_date = get_date();
    }else{
        $current_date = date("Y-m-d",strtotime("-1 day"));
    }
    return $current_date;
}




